/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.basic.hazard.source.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.basic.hazard.source.bo.HazMonitorFactorAndDataBO;
import com.hopes.bizz.basic.hazard.source.dto.HazMonitorFactorDTO;
import com.hopes.bizz.basic.hazard.source.dto.HazMonitorFactorExcelDTO;
import com.hopes.bizz.basic.hazard.source.entity.HazHazardSourceEntity;
import com.hopes.bizz.basic.hazard.source.entity.HazMonitorFactorEntity;
import com.hopes.bizz.basic.hazard.source.mapper.HazMonitorFactorMapper;
import com.hopes.bizz.basic.hazard.source.service.HazHazardSourceService;
import com.hopes.bizz.basic.hazard.source.service.HazMonitorFactorService;
import com.hopes.bizz.basic.hazard.source.vo.HazMonitorFactorExcelVO;
import com.hopes.bizz.hazard.common.api.feign.RemoteHazardDictService;
import com.hopes.bizz.hazard.common.constant.enums.HazardModDictEnum;
import com.hopes.bizz.hazard.om.entity.HazRtDataEntity;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.common.core.constant.SecurityConstants;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 危险源监控因子设置
 *
 * @author daijie
 * @date 2023-11-01 17:32:11
 */
@Service
public class HazMonitorFactorServiceImpl extends ServiceImpl<HazMonitorFactorMapper, HazMonitorFactorEntity> implements HazMonitorFactorService {

    @Autowired
    @Lazy
    private HazHazardSourceService hazHazardSourceService;

    @Autowired
    private RemoteHazardDictService remoteHazardDictService;

    @Override
    public ValidateImportBO<HazMonitorFactorExcelDTO> validateImport(List<HazMonitorFactorExcelDTO> excelDTOList) {
        ValidateImportBO<HazMonitorFactorExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<HazMonitorFactorExcelDTO> saveList = new ArrayList<>(excelDTOList);
        int successNum = excelDTOList.size();
        int failedNum = 0;

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public HazMonitorFactorEntity importDtoToEntity(HazMonitorFactorExcelDTO excelDto) {
        HazMonitorFactorEntity entity= new HazMonitorFactorEntity();
        BeanUtils.copyProperties(excelDto, entity);
        return entity;
    }

    @Override
    public HazMonitorFactorExcelVO exportEntityToVo(HazMonitorFactorEntity entity) {
        HazMonitorFactorExcelVO excelVO = new HazMonitorFactorExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        excelVO.setFactorIdStr(DictResolver.getItemLabelByValue(HazardModDictEnum.factor_config, entity.getFactorId()));
        excelVO.setMonitorSiteIdStr( DictResolver.getItemLabelByValue(HazardModDictEnum.rtu, entity.getMonitorSiteId()));
        return excelVO;
    }

    @Override
    public List<HazMonitorFactorAndDataBO> getAllFactorAndData(HazMonitorFactorEntity entity) {
        List<HazMonitorFactorAndDataBO> allFactorAndData = this.baseMapper.getAllFactorAndData(entity);
        for (HazMonitorFactorAndDataBO allFactorAndDatum : allFactorAndData) {
            HazRtDataEntity rtData = remoteHazardDictService.getByMonitorFactorId(allFactorAndDatum.getId(), SecurityConstants.FROM_IN).getData();
            if (rtData != null) {
                allFactorAndDatum.setMonitorData(rtData.getMonitorData());
                allFactorAndDatum.setDataTime(rtData.getDataTime());
                allFactorAndDatum.setFactorCode(rtData.getFactorCode());
            }
        }

        return allFactorAndData;
    }

    @Override
    public List<Tree<Long>> monitorFactorTree(HazMonitorFactorDTO hazMonitorFactor) {
        List<HazHazardSourceEntity> hazardSourceList = hazHazardSourceService.list();
        // 一级为危险源
        List<TreeNode<Long>> tree = hazardSourceList.stream().map(hazHazardSource -> {
            TreeNode<Long> treeNode = new TreeNode<>();
            treeNode.setId(hazHazardSource.getId());
            treeNode.setParentId(0L);
            treeNode.setName(String.format("%s(%s)", hazHazardSource.getHazardName(), hazHazardSource.getHazardSourceLevelStr()));
            treeNode.setWeight(Integer.parseInt(hazHazardSource.getHazardSourceLevel()));

            return treeNode;
        }).collect(Collectors.toList());

        List<HazMonitorFactorEntity> monitorFactorList = this.list();

        // 二级为存储场所
        tree.addAll(monitorFactorList.stream().map(monitorFactor -> {
            TreeNode<Long> treeNode = new TreeNode<>();
            treeNode.setId(monitorFactor.getStorageId());
            treeNode.setParentId(monitorFactor.getHazardSourceId());
            treeNode.setName(monitorFactor.getStorageIdStr());
            treeNode.setWeight(monitorFactor.getSortNum());

            return treeNode;
        }).collect(Collectors.toList()));

        // 三级为监控因子
        tree.addAll(monitorFactorList.stream().map(monitorFactor -> {
            TreeNode<Long> treeNode = new TreeNode<>();
            treeNode.setId(monitorFactor.getId());
            treeNode.setParentId(monitorFactor.getStorageId());
            treeNode.setName(monitorFactor.getFactorName());
            treeNode.setWeight(monitorFactor.getSortNum());

            return treeNode;
        }).collect(Collectors.toList()));

        return TreeUtil.build(tree, 0L);
    }
}
